<?php

namespace App\Http\Controllers\Admin\Wechat;

use App\Http\Controllers\Admin\BaseController;

use App\Http\Requests\Admin\Wechat\CreateRequest;
use App\Http\Requests\Admin\Wechat\UpdateRequest;
use App\Repositories\ReplyRepositoryEloquent;
use App\Models\Reply;
use Illuminate\Http\Request;
use Toastr;

class ReplyController extends BaseController
{   

    protected $reply;

    protected $adminUserRole;

    public function __construct(ReplyRepositoryEloquent $reply)
    {
        parent::__construct();
        $this->reply = $reply;
    }

    public function index(){
        $replies = Reply::where('msgtype','=','text')->get();
        return view('admin.wechat.reply.index',compact('replies','replies'));
    }

    public function news(){
        $replies = Reply::where('msgtype','=','news')->get();
        return view('admin.wechat.reply.news',compact('replies','replies'));
    }

    public function createText(){
        return view('admin.wechat.reply.create-text');
    }

    public function createNews(){
        return view('admin.wechat.reply.create-news');
    }

    public function store(CreateRequest $request){
        $type = $request['msgtype'];
        $picurl = '';
        if($request->hasFile('picurl')){
            if ($request->file('picurl')->isValid()){
            //if (1){
                $images=$request->file('picurl'); //使用laravel 自带的request类来获取一下文件.
                $filedir="upload/admin/wechat/reply/"; //定义图片上传路径
                $imagesName=$images->getClientOriginalName(); //获取上传图片的文件名
                $extension = $images->getClientOriginalExtension();
                //$images->move($filedir,$imagesName); //使用move 方法移动文件.
                $newImagesName=md5(time()).random_int(5,5).".".$extension; //重新命名上传文件名字
                $images->move($filedir,$newImagesName); //使用move 方法移动文件.
                $picurl =$filedir.$newImagesName ;
            }else{
                Toastr::error('封面上传失败!');
                return redirect(route('admin.wechat.reply.create-'.$type));
            }
        }

        $result = Reply::create([
            'name'=>$request['name'],
            'msgtype'=>$request['msgtype'],
            'keyword'=>$request['keyword'],
            'regular'=>$request['regular'],
            'title'=>$request['title'],
            'picurl'=>$picurl,
            'musicurl'=>$request['musicurl'],
            'hqmusicurl'=>$request['hqmusicurl'],
            'mediaid'=>$request['mediaid'],
            'url'=>$request['url'],
            'thumbmediaid'=>$request['thumbmediaid'],
            'description'=>$request['description'],
            'content'=>$request['content'],
            'attributes'=>$request['attributes']
        ]);
        if(!$result){
            Toastr::error('新回复添加失败!');
            return redirect(route("admin.wechat.reply.create-".$type));
        }
        Toastr::success('新回复添加成功!');
        if($type=="news"){
            return redirect('admin/wechat/reply/news');
        }else{
            return redirect('admin/wechat/reply');
        }
        
    }

    public function edit($id){
        $reply = Reply::find($id);
        //dd($reply);
        return view("admin.wechat.reply.edit-{$reply->msgtype}",compact('reply','reply'));
    }

    public function show(){

    }

    public function update(UpdateRequest $request, $id){
        //$type=$request['msgtype'];
        $data = [
            'name'=>$request['name'],
            'msgtype'=>$request['msgtype'],
            'keyword'=>$request['keyword'],
            'regular'=>$request['regular'],
            'title'=>$request['title'],
            //'picurl'=>$request['picurl'],
            'musicurl'=>$request['musicurl'],
            'hqmusicurl'=>$request['hqmusicurl'],
            'mediaid'=>$request['mediaid'],
            'url'=>$request['url'],
            'thumbmediaid'=>$request['thumbmediaid'],
            'description'=>$request['description'],
            'content'=>$request['content'],
            'attributes'=>$request['attributes']
        ];

        if($request->hasFile('picurl')){
            if ($request->file('picurl')->isValid()){
            //if (1){
                $images=$request->file('picurl'); //使用laravel 自带的request类来获取一下文件.
                $filedir="upload/admin/wechat/reply/"; //定义图片上传路径
                $imagesName=$images->getClientOriginalName(); //获取上传图片的文件名
                $extension = $images->getClientOriginalExtension();
                //$images->move($filedir,$imagesName); //使用move 方法移动文件.
                $newImagesName=md5(time()).random_int(5,5).".".$extension; //重新命名上传文件名字
                $images->move($filedir,$newImagesName); //使用move 方法移动文件.
                $data['picurl'] =$filedir.$newImagesName ;
            }else{
                Toastr::error('封面上传失败!');
                return redirect(route('admin.wechat.reply.edit'));
            }
        }

        //$data = $request->all();
        //dd($data);
        $isAble = Reply::where('id', '<>', $id)->where('name', $data['name'])->count();
        if($isAble) {
           $result= [
                'status' => false,
                'msg' => '规则名称已被使用'
            ];
            Toastr::error($result['msg']);
        }else{
            $res = Reply::where('id','=',$id)->update($data);
            if(!$res) {
                $result= [
                    'status' => false,
                    'msg' => '回复更新失败'
                ];
                Toastr::error($result['msg']);
            }else{
                Toastr::success('回复更新成功');
            }
            
        }

        // if(!$result['status']) {
        //         Toastr::error($result['msg']);
        // } else {
        //     Toastr::success('回复更新成功');
        // }

        
        return redirect(route('admin.wechat.reply.edit', ['id' => $id]));
        //return redirect('admin/wechat/reply');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

        $result = $this->reply->delete($id);
        return response()->json($result ? ['status' => 1] : ['status' => 0]);
    }

    /**
     * Delete multi users
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroyAll(Request $request)
    {
        if(!($ids = $request->get('ids', []))) {
            return response()->json(['status' => 0, 'msg' => '请求参数错误']);
        }

        foreach($ids as $id){
            $result = $this->reply->delete($id);
        }
        return response()->json($result ? ['status' => 1] : ['status' => 0]);
    }

}